package system_design.home.jiekou;

import system_design.home.shiti.Goods;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LENOVO
 * Date: 2022-01-07
 * Time: 9:10
 */
public class DelOperation implements IOperation {
    public void work(GoodsList goodsList) {
        System.out.println("删除商品");
        System.out.println("请输入要删除的商品号");
        int goodsNum = input.nextInt();
        int size = goodsList.getUsedSize();
        int index = 0;//存储找到的下标
        int i = 0;
        for (; i < size - 1; i++) {
            Goods goods = goodsList.getGoods(i);//寻找所有商品
           if(goodsNum == goods.getGoodsNum()){
                index = i;//找到的要删除的下标
                break;
            }
        }
        if(i >= size){
            System.out.println("没有你要删除的商品");
             return;
        }
         for (int j = 0; j < size - 1; j++) {
             Goods goods = goodsList.getGoods(j + 1);//获得j后面的商品
             goodsList.setGoods(j,goods);//让j位置的商品被j+1位置的商品所覆盖
        }
        goodsList.setGoods(size,null);//让最后一件商品置空
        goodsList.setUsedSize(size - 1);//让有效元素-1
    }
}
